/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Initialisation state variables.
 */
var libsLoaded = false;
var modeAvailable = false;
var stateAvailable = false;

/** Choices to show in gadget. */
var CHOICES = [ {
  value : 'draft',
  label : 'Draft',
  color : '#d00'
}, {
  value : 'review',
  label : 'For Review',
  color : '#fe0'
}, {
  value : 'final',
  label : 'Final',
  color : '#080'
} ];

var COLORS = {};
for ( var i = 0; i < CHOICES.length; i++) {
  COLORS[CHOICES[i].value] = CHOICES[i].color;
}

/**
 * Initialise list with all possible choices
 */
function addChoices() {
  wave.log('Adding choices: ' + CHOICES);
  var ol = $('ol');
  for ( var i = 0; i < CHOICES.length; i++) {
    var choice = CHOICES[i];
    ol.append('<li id="' + choice.value + '"><div>' + choice.label
        + '</div></li>');
    var newLi = ol.find('li:last-child');
    newLi.data('value', choice.value);
  }

  // On click, set new state into wave.
  $('li').click(function(event) {
      if ($('body').hasClass('edit')) {
        wave.getState().submitValue('choice', $(this).data('value'))
      }
      return false;
    }
  );
  gadgets.window.adjustHeight();
}


/**
 * Wave mode update callback.
 */
function onModeUpdated() {
  if (!isGadgetReady()) {
    modeAvailable = true;
    checkGadgetReady();
    return;
  }

  if (wave.getMode() == wave.Mode.EDIT) {
    $('body').addClass('edit');
  } else {
    $('body').removeClass('edit');
  }
}

/**
 * Wave status update callback.
 */
function onWaveUpdated() {
  if (!isGadgetReady()) {
    if (wave.getState() != null) {
      stateAvailable = true;
      checkGadgetReady();
    }
    return;
  }

  var choice = wave.getState().get('choice', CHOICES[0].value);

  var prevOn = $('li.on');
  $('li').removeClass('on');
  var currOn = $('#' + choice)
  currOn.addClass('on');

  // Ensure mover created
  var mover = $('#mover');
  if (mover.size() == 0) {
    mover = $('<div id="mover"></div>').appendTo('body');
  }

  // Set mover to correct height and width
  mover.stop().css('width', currOn.width());
  mover.css('top', currOn.offset().top + currOn.outerHeight);

  if (prevOn.size() == 0) {
    // Set to final size now
    mover.css('left', currOn.offset().left);
    mover.css('backgroundColor', COLORS[choice]);
  } else {
    mover.css('left', prevOn.offset().left);
    mover.css('backgroundColor', COLORS[prevOn.get(0).id]);
    mover.animate( {
      left : currOn.offset().left,
      backgroundColor : COLORS[choice]
    }, 500);
  }
}

/***** Gadget initialisation functions. *****/

/**
 * Determines whether gadget is ready to initialise itself.
 */
function isGadgetReady() {
  return libsLoaded && modeAvailable && stateAvailable;
}


/**
 * Called to determine gadget is ready and finish initialisation.
 */
function checkGadgetReady() {
  if (!isGadgetReady()) {
    return;
  }
  onModeUpdated();
  onWaveUpdated();
}


gadgets.util.registerOnLoadHandler(function() {
  addChoices();
  wave.setStateCallback(onWaveUpdated);
  wave.setModeCallback(onModeUpdated);
});

google.setOnLoadCallback(function() {
  libsLoaded = true;
  checkGadgetReady();
});
